package com.wpre.lotterysystem.controller;

import com.wpre.lotterysystem.common.errcode.ControllerErrorCodeConstants;
import com.wpre.lotterysystem.common.exception.ControllerException;
import com.wpre.lotterysystem.common.pojo.CommonResult;
import com.wpre.lotterysystem.common.utils.JacksonUtil;
import com.wpre.lotterysystem.controller.param.CreateActivityParam;
import com.wpre.lotterysystem.controller.param.PageParam;
import com.wpre.lotterysystem.controller.result.CreateActivityResult;
import com.wpre.lotterysystem.controller.result.FindActivityListResult;
import com.wpre.lotterysystem.controller.result.GetActivityDetailResult;
import com.wpre.lotterysystem.service.ActivityService;
import com.wpre.lotterysystem.service.dto.ActivityDTO;
import com.wpre.lotterysystem.service.dto.ActivityDetailDTO;
import com.wpre.lotterysystem.service.dto.CreateActivityDTO;
import com.wpre.lotterysystem.service.dto.PageListDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Comparator;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-12-21
 * Time: 15:22
 */
@Slf4j
@RestController
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    //创建活动
    @RequestMapping("/activity/create")
    public CommonResult<CreateActivityResult> createActivity(@RequestBody @Validated CreateActivityParam param) {
        log.info("createActivity CreateActivityParam {}", JacksonUtil.writeValueAsString(param));
        return CommonResult.success(convertToCreateActivityResult(activityService.createActivity(param)));
    }

    //查询活动列表
    @RequestMapping("/activity/find-list")
    //因为是支持翻页功能,所以参数是PageParam
    public CommonResult<FindActivityListResult> findActivityList(PageParam param) {

        log.info("findActivityList PageParam {}", JacksonUtil.writeValueAsString(param));
        return CommonResult.success(convertTofindActivityListResult(activityService.findActivityList(param)));

    }

    //获取活动详细信息
    @RequestMapping("/activity-detail/find")
    public CommonResult<GetActivityDetailResult> getActivityDetail(Long activityId) {
        log.info("getActivityDetail activityId:{}", activityId);
        ActivityDetailDTO activityDetailDTO = activityService.getActivityDetail(activityId);
        return CommonResult.success(convertToGetActivityDetailResult(activityDetailDTO));
    }

    private GetActivityDetailResult convertToGetActivityDetailResult(ActivityDetailDTO activityDetailDTO) {
        if (activityDetailDTO == null) {
            throw new ControllerException(ControllerErrorCodeConstants.GET_ACTIVITY_DETAIL_LIST_ERROR);
        }
        GetActivityDetailResult result = new GetActivityDetailResult();
        result.setActivityId(activityDetailDTO.getActivityId());
        result.setActivityName(activityDetailDTO.getActivityName());
        result.setDescription(activityDetailDTO.getDesc());
        result.setValid(activityDetailDTO.valid());
        //规定抽奖顺序:先抽一等奖,再抽二等奖,再抽三等奖
        result.setPrizes(activityDetailDTO.getPrizeDTOList()
                .stream()
                .sorted(Comparator.comparingInt(prizeDTO -> prizeDTO.getTiers().getCode()))  //根据奖品等级排序
                .map(prizeDetailDTO -> {
                    GetActivityDetailResult.Prize prize = new GetActivityDetailResult.Prize();
                    prize.setPrizeId(prizeDetailDTO.getPrizeId());
                    prize.setName(prizeDetailDTO.getName());
                    prize.setImageUrl(prizeDetailDTO.getImageUrl());
                    prize.setPrice(prizeDetailDTO.getPrice());
                    prize.setDescription(prizeDetailDTO.getDescription());
                    prize.setPrizeTierName(prizeDetailDTO.getTiers().getMessage());
                    prize.setPrizeAmount(prizeDetailDTO.getPrizeAmount());
                    prize.setValid(prizeDetailDTO.valid());
                    return prize;
                }).collect(Collectors.toList())
        );
        result.setUsers(activityDetailDTO.getUserDetailDTOList()
                .stream()
                .map(userDetailDTO -> {
                    GetActivityDetailResult.User user = new GetActivityDetailResult.User();
                    user.setUserId(userDetailDTO.getUserId());
                    user.setUserName(userDetailDTO.getUserName());
                    user.setValid(userDetailDTO.valid());
                    return user;
                }).collect(Collectors.toList())
        );
        return result;
    }


    private FindActivityListResult convertTofindActivityListResult(PageListDTO<ActivityDTO> activityList) {
        if (activityList == null) {
            throw new ControllerException(ControllerErrorCodeConstants.FIND_ACTIVITY_LIST_ERROR);
        }

        FindActivityListResult result = new FindActivityListResult();
        result.setTotal(activityList.getTotal());
        result.setRecords(activityList.getRecords()
                .stream().
                map(activityDTO -> {
                    FindActivityListResult.ActivityInfo activityInfo = new FindActivityListResult.ActivityInfo();
                    activityInfo.setActivityId(activityDTO.getActivityId());
                    activityInfo.setActivityName(activityDTO.getActivityName());
                    activityInfo.setDescription(activityDTO.getDescription());
                    activityInfo.setValid(activityDTO.valid());
                    return activityInfo;
                }).collect(Collectors.toList()));


        return result;
    }


    private CreateActivityResult convertToCreateActivityResult(CreateActivityDTO createActivityDTO) {
        if (createActivityDTO == null) {
            throw new ControllerException(ControllerErrorCodeConstants.CREATE_ACTIVITY_ERROR);
        }
        CreateActivityResult result = new CreateActivityResult();
        result.setActivityId(createActivityDTO.getActivityId());
        return result;
    }
}
